﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;

namespace Commodity
{
    public partial class Commodityread : Skin_Mac
    {
        public Commodityread()
        {
            InitializeComponent();
        }
        public DataTable dt;
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量
        string str1 = "select a.CommodityID,a.CommodityName,b.TypeName,a.OrderGoodsPrice,a.SellGoodsPrice,a.Colour,c.SupplierName,a.introduce from Commodity a join CommodityType b on a.TypeID=b.TypeID join Supplier c on a.SupplierID=c.SupplierID";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[Commodity]");//把数据库表中的i-j的记录数用来填充数据集
            Commoditydgv.DataSource = ds.Tables["[Commodity]"];
            ds = null;//清空数据集
        }
        private void Commodityread_Load(object sender, EventArgs e)
        {
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据

            //商品类型下拉框绑定     
            string sqltp = "select distinct TypeName from commodityType";
            DataTable dte = DBHelper.GetDataTable(sqltp);
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                tpcmb.Items.Add(dte.Rows[i][0]);
            }
        }
        //退出
        private void exitbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void udtbt_Click_1(object sender, EventArgs e)
        {
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            this.tpcmb.Text = "";
        }

        

        private void backbt_Click_1(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页......");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        private void nextbt_Click_1(object sender, EventArgs e)
        {
            if (start + 1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        

        private void readbt_Click_2(object sender, EventArgs e)
        {
            start = 0;//初始化页数
            string tpnm = tpcmb.Text;
            string tpid = null;
            if (tpnm != "")
            {
                string tpsql = string.Format("select TypeID from CommodityType where TypeName='{0}'", tpnm);
                tpid = DBHelper.GetDataTable(tpsql).Rows[0][0].ToString();
                string sql = string.Format("select a.CommodityID,a.CommodityName,b.TypeName,a.OrderGoodsPrice,a.SellGoodsPrice,a.Colour,a.introduce,c.SupplierName from Commodity a join CommodityType b on a.TypeID=b.TypeID join Supplier c on a.SupplierID=c.SupplierID where a.TypeID='{0}'", Convert.ToInt32(tpid));
                DataTable dt = DBHelper.GetDataTable(sql);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + tpnm + "的订单");
                }
                else
                {
                    show(0, size);
                    plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
                Commoditydgv.DataSource = dt;
            }
            else if (tpnm == "")
            {
                MessageBoxEx.Show("请输入关键字");
            }
        }
    }
}
